package com.binhthuan.catechist.server.bus;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.binhthuan.catechist.server.domain.Schedule;
import com.binhthuan.catechist.server.domain.ScheduleStatus;
import com.binhthuan.catechist.server.util.HibernateUtil;

public class ScheduleBUS extends AbstractBUS {

    public ScheduleStatus getScheduleStatusById(int id) {
	return (ScheduleStatus) get(ScheduleStatus.class, id);
    }

    @SuppressWarnings("unchecked")
    public List<Schedule> getScheduleByClass(int doctrineClassId) {
	Session session = HibernateUtil.getSessionFactory().openSession();
	Transaction transaction = null;
	List<Schedule> result = null;

	try {
	    transaction = session.beginTransaction();
	    Query query = session
		    .createQuery("from Schedule s where s.doctrineClass.id = :classId");
	    query.setInteger("classId", doctrineClassId);
	    result = new ArrayList<Schedule>(query.list());

	    transaction.commit();
	} catch (HibernateException he) {
	    he.printStackTrace();
	    transaction.rollback();
	} finally {
	    session.close();
	}

	return result;
    }

    public Schedule getScheduleById(int id) {
        return (Schedule) get(Schedule.class, id);
    }

    public boolean deleteSchedule(int id) {
        return delete(Schedule.class, id);
    }
}
